#######################################
##      Replicating Table 2          ##        
##   in Hamel and Miller (2018)      ##
#######################################

install.packages(c("Matching", "rgenoud"))
library(Matching)
library(rgenoud)

scandal <- read.csv("scandal_dta.csv")

## All Scandals

attach(scandal)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match <- GenMatch(Tr = scandal_id, X = covariates_match, 
                            BalanceMatrix = covariates_balance, M = 3, 
                            pop.size = 12500, max.generations = 10, wait.generations = 5, 
                            hard.generation.limit = FALSE, int.seed = 444, unif.seed = 636, 
                            fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE)

effect_vote <- Match(Y = votepct, Tr = scandal_id, 
                               X = covariates_match, M = 3, 
                               exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                               replace = TRUE, ties = FALSE, version = "fast", 
                               Weight.matrix = match)
summary(effect_vote)

effect_win <- Match(Y = win, Tr = scandal_id, 
                      X = covariates_match, M = 3, 
                      exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                      replace = TRUE, ties = FALSE, version = "fast", 
                      Weight.matrix = match, 
                      match.out = effect_vote)
summary(effect_win)

effect_money <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                X = covariates_match, M = 3, 
                                exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                replace = TRUE, ties = FALSE, version = "fast", 
                                Weight.matrix = match, 
                                match.out = effect_vote)
summary(effect_money)

balance <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                    dwnom1 + seniority + les + dpres + 
                                    logged_pre_sum_infl + chall_quality, 
                                  match.out = effect_vote,
                                  nboots = 5000)

detach(scandal)

## Covered Scandals

scandal_covered <- subset(scandal, covered == 1)

attach(scandal_covered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_covered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                    BalanceMatrix = covariates_balance, M = 3, 
                                    pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                    hard.generation.limit = FALSE, int.seed = 151, unif.seed = 234, 
                                    fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                    replace = TRUE, ties = FALSE)

effect_vote_covered <- Match(Y = votepct, Tr = scandal_id, 
                                       X = covariates_match, M = 3, 
                                       exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE, version = "fast", 
                                       Weight.matrix = match_covered)
summary(effect_vote_covered)


effect_win_covered <- Match(Y = win, Tr = scandal_id, 
                              X = covariates_match, M = 3, 
                              exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                              replace = TRUE, ties = FALSE, version = "fast", 
                              Weight.matrix = match_covered, 
                              match.out = effect_vote_covered)
summary(effect_win_covered)


effect_money_covered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                        X = covariates_match, M = 3, 
                                        exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                        replace = TRUE, ties = FALSE, version = "fast", 
                                        Weight.matrix = match_covered, 
                                        match.out = effect_vote_covered)
summary(effect_money_covered)



balance_covered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                            dwnom1 + seniority + les + dpres + 
                                            logged_pre_sum_infl + chall_quality, 
                                          match.out = effect_vote_covered,
                                          nboots = 5000)
detach(scandal_covered)

## Non-Covered Only

scandal_noncovered <- subset(scandal, covered == 0)

attach(scandal_noncovered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_noncovered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                       BalanceMatrix = covariates_balance, M = 3, 
                                       pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                       hard.generation.limit = FALSE, int.seed = 153, unif.seed = 834, 
                                       fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE)

effect_vote_noncovered <- Match(Y = votepct, Tr = scandal_id, 
                                          X = covariates_match, M = 3, 
                                          exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                          replace = TRUE, ties = FALSE, version = "fast", 
                                          Weight.matrix = match_noncovered)
summary(effect_vote_noncovered)

effect_win_noncovered <- Match(Y = win, Tr = scandal_id, 
                                 X = covariates_match, M = 3, 
                                 exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                 replace = TRUE, ties = FALSE, version = "fast", 
                                 Weight.matrix = match_noncovered, 
                                 match.out = effect_vote_noncovered)
summary(effect_win_noncovered)

effect_money_noncovered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                           X = covariates_match, M = 3, 
                                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                           replace = TRUE, ties = FALSE, version = "fast", 
                                           Weight.matrix = match_noncovered, 
                                           match.out = effect_vote_noncovered)
summary(effect_money_noncovered)

balance_noncovered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                               dwnom1 + seniority + les + dpres + 
                                               logged_pre_sum_infl + chall_quality, 
                                             match.out = effect_vote_noncovered,
                                             nboots = 5000)

detach(scandal_noncovered)
